package ml.stayfool.configuration;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.dataformat.yaml.YAMLFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by pactera on 2017/1/8.
 */
public class ConfigProps {

    private static final ConfigProps props;

    private static String api = "http://api.map.baidu.com/geocoder/v2/" +
            "?ak={ak}&location={location}&output=json&pois=0&coordtype=wgs84ll";

    private static Logger logger = LoggerFactory.getLogger(ConfigProps.class);

    static {
        ConfigProps tmpProps;
        ObjectMapper mapper = new ObjectMapper(new YAMLFactory());
        try {
            InputStream is = Thread.currentThread().getContextClassLoader()
                    .getResourceAsStream("app.yml");
            tmpProps = mapper.readValue(is, ConfigProps.class);
            api = api.replace("{ak}", tmpProps.getAk());
            logger.info("load configuration successed");
        } catch (Exception e) {
            tmpProps = new ConfigProps();
            logger.error("load configuration failed", e);
        }
        props = tmpProps;
    }

    private List<String> imgdirs = new ArrayList<>(1);

    private String outdir;

    private String ak;

    public List<String> getImgdirs() {
        return imgdirs;
    }

    public void setImgdirs(List<String> imgdirs) {
        this.imgdirs = imgdirs;
    }

    public String getOutdir() {
        return outdir;
    }

    public void setOutdir(String outdir) {
        this.outdir = outdir;
    }

    public String getAk() {
        return ak;
    }

    public void setAk(String ak) {
        this.ak = ak;
    }

    public static List<String> imgdirs() {
        return props.getImgdirs();
    }

    public static String outdir() {
        return props.getOutdir();
    }

    public static String getApiUrl(String locatioin) {
        return api.replace("{location}", locatioin);
    }
}
